<?php
/**
 * Created by PhpStorm.
 * User: tony
 * Date: 2018/6/4
 * Time: 11:20
 */

namespace app\modules\v1\controllers;

use app\config\Status;
use app\modules\v1\controllers\parent\ParentController;
use app\models\Source;
use yii\data\ActiveDataProvider;

class SourceController extends ParentController
{
    public function actionIndex(){
        $params = \Yii::$app->request->getQueryParams();
        $query = Source::find()->with('branch')->with('user');
        if(isset($params['started_at']) && isset($params['ended_at']) && $params['started_at']!='0' && $params['started_at']!='0')
        {
            $query->andWhere(['>=', 'meeting_date',$params['started_at']]);
            $query->andWhere(['<=', 'meeting_date',$params['ended_at']]);
        }
        if(isset($params['branch_id']) && $params['branch_id'] != '0')
            $query->andWhere(['branch_id'=>$params['branch_id']]);
        if(isset($params['name']) && $params['branch_id'] !== '')
            $query->andWhere(['like','name', $params['name']]);

        $pageSize = 6;
        if(isset($params['pageSize'])){
            $pageSize = (int)$params['pageSize'];
        }
        return new ActiveDataProvider([
            // 使用with方法实现贪加载
            'query' => $query,
            // 分页信息
            'pagination' => [
                'pageSize' => $pageSize,
            ],
            // 排序信息
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                    'branch_id' => SORT_ASC,
                ]
            ],
        ]);
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionCreate(){
        $params = \Yii::$app->request->getBodyParams();
        $source = new Source();
        $source->load($params,'');
        $source->created_at = $source->updated_at = time();
        $source->user_id = \Yii::$app->user->id;
        if($source->save()){
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $source
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => $source->errors
        ];
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionView(){
        $params = \Yii::$app->request->get('id');
        $id = (int)$params;
        $source = Source::findOne($id);
        return $source;
    }

    /**
     * @throws \yii\base\InvalidConfigException
     */
    public function actionUpdate(){
        $params = \Yii::$app->request->getBodyParams();
        $id = (int)$params['id'];
        $source = Source::findOne($id);
        if($source){

            if(!$this->isMine($source->user_id)){
                return [
                    'bCode' => Status::FAIL,
                    'bData' => ['msg'=>'你只能修改自己创建的会议记录']
                ];
            }

            $source->load($params,'');
            $source->updated_at = time();
            if($source->save()){
                return [
                    'bCode' => Status::SUCCESS,
                    'bData' => $source
                ];
            }else{
                return [
                    'bCode' => Status::FAIL,
                    'bData' => $source->errors
                ];
            }
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => '该会议记录ID不存在'
        ];
    }

    /**
     * @return array
     * @throws \Throwable
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete(){
        $params = \Yii::$app->request->getBodyParams();
        $id = (int)$params['id'];

        $source = Source::findOne($id);
        if($source){
            // 检查权限
            if(!$this->isMine($source->user_id)){
                return [
                    'bCode' => Status::FAIL,
                    'bData' => ['msg'=>'你只能删除自己创建的会议记录']
                ];
            }
            $source->delete();
            return [
                'bCode' => Status::SUCCESS,
                'bData' => $source
            ];
        }
        return [
            'bCode' => Status::FAIL,
            'bData' => '该会议记录ID不存在'
        ];
    }
}